package com.rightsidetech.http.controller.admin.mall;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.activity.service.ActGiftService;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.mall.dto.request.TypePageReq;
import com.rightsidetech.mall.dto.request.TypeReq;
import com.rightsidetech.mall.dto.request.TypeUpReq;
import com.rightsidetech.mall.dto.response.TypePageResp;
import com.rightsidetech.mall.service.MallTypeService;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/mall_type/")
@ShenyuSpringMvcClient(path = "/admin/mall_type/**")
@Api(tags = "Admin:商品分类相关")
public class MallTypeController extends BaseController {

    @Resource
    private MallTypeService mallTypeService;
    @Resource
    private ActGiftService actGiftService;

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/save")
    @ApiOperation("添加分类")
    public ResponseData save(@RequestBody TypeReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if(null == loginInfoId || null == loginName){
            return ResponseData.fail("未找到登录人");
        }
        return mallTypeService.save(req,loginName,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/update")
    @ApiOperation("编辑分类")
    public ResponseData updateType(@RequestBody TypeUpReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if(null == loginInfoId || null == loginName){
            return ResponseData.fail("未找到登录人");
        }
        return mallTypeService.update(req,loginName,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/del")
    @ApiOperation("删除分类")
    public ResponseData deleteType(@RequestBody DelReq req){
        return mallTypeService.isDelete(req);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    @ApiOperation("分页查询分类")
    public ResponseData<PageInfo<TypePageResp>> pageType(TypePageReq req){
         return mallTypeService.page(req);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/child")
    @ApiOperation("查询子级分类")
    public ResponseData<List<TypePageResp>> getChild(@RequestParam(value = "id",required = true) Long id){
        return mallTypeService.getChildList(id);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/all")
    @ApiOperation("获取所有的分类")
    public ResponseData<List<TypePageResp>> getAll(){
         return mallTypeService.getAll();
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/act_type")
    @ApiOperation("分类关联换购活动")
    public ResponseData<List<TypePageResp>> actType(@RequestParam(value = "actGiftId",required = true) Long actGiftId){
        ResponseData<List<TypePageResp>> all = mallTypeService.getAll();
        List<TypePageResp> resData = all.getResData();
        List<TypePageResp> result = new ArrayList<>();
        result = actAndType(result,resData);
        List<Long> typeIds = actGiftService.getTypeIdsByActId(actGiftId);
        for (TypePageResp resp : result) {
            for (Long id : typeIds) {
                if(resp.getId().equals(id)){
                    resp.setType((byte)1);
                }
            }
        }
        return all;
    }

    private List<TypePageResp> actAndType(List<TypePageResp> result,List<TypePageResp> data){
        for (TypePageResp resp : data) {
            if(CollUtil.isEmpty(resp.getChildren())){
                result.add(resp);
            }else {
                actAndType(result,resp.getChildren());
            }
        }
        return result;
    }
}
